package bean;

import java.io.File;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ComponentSystemEvent;
import javax.servlet.ServletContext;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.property.Setter;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.event.DragDropEvent;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

import Model.HoaDon;
import Model.NguoiDung;
import Model.NhaSanXuat;
import Model.SanPham;
import Util.DAO;
import Util.HibernateUtil;
import Util.SanPhamDataModel;



@ManagedBean
@SessionScoped
public class BeanBanHang implements Serializable{
	private static List<SanPham> dssp = new ArrayList<SanPham>();
	private List<SanPham> giohang = new ArrayList<SanPham>();
	
	public MenuModel model;
	private SanPham selectedSanPham;
	private SanPham selectedSua;
	private Map<SanPham, Boolean> mapSua = new HashMap<SanPham, Boolean>();
	public static  String tieude;
	private SanPham selectedSanPhamGioHang;
	public long tongsotien = 0;
	private SanPhamDataModel dataModel;
	private NguoiDung nguoidung = new NguoiDung();
	public NguoiDung getNguoidung() {
		return nguoidung;
	}
	public void setNguoidung(NguoiDung nguoidung) {
		this.nguoidung = nguoidung;
	}
	
	static{
		tieude = "Tất Cả Sản Phẩm";
		dssp = DAO.laytatcasanpham();
	}
	public SanPham getSelectedSanPhamGioHang() {
		return selectedSanPhamGioHang;
	}
	public void setSelectedSanPhamGioHang(SanPham selectedSanPhamGioHang) {
		this.selectedSanPhamGioHang = selectedSanPhamGioHang;
	}
	public SanPhamDataModel getDataModel() {
		return dataModel;
	}
	public void setDataModel(SanPhamDataModel dataModel) {
		this.dataModel = dataModel;
	}
	public MenuModel getModel() {
		return model;
	}
	public void setModel(MenuModel model) {
		this.model = model;
	}
	public SanPham getSelectedSanPham() {
		return selectedSanPham;
	}
	public void setSelectedSanPham(SanPham selectedSanPham) {
		this.selectedSanPham = selectedSanPham;
	}
	
	public String getTieude() {
		return tieude;
	}
	public void setTieude(String tieude) {
		this.tieude = tieude;
	}
	public List<SanPham> getGiohang() {
		return giohang;
	}
	public void setGiohang(List<SanPham> giohang) {
		this.giohang = giohang;
	}
	
	
	public List<SanPham> getDssp() {
		return dssp;
	}
	public void setDssp(List<SanPham> dssp) {
		this.dssp = dssp;
	}
	
	public Map<SanPham, Boolean> getMapSua() {
		return mapSua;
	}
	public void setMapSua(Map<SanPham, Boolean> mapSua) {
		this.mapSua = mapSua;
	}
	
	
	public SanPham getSelectedSua() {
		return selectedSua;
	}
	public void setSelectedSua(SanPham selectedSua) {
		this.selectedSua = selectedSua;
	}
	public BeanBanHang(){
		FacesContext facesCtx = FacesContext.getCurrentInstance();
		
		System.out.println(facesCtx.getExternalContext().getSessionMaxInactiveInterval());
		
		ELContext elCtx = facesCtx.getELContext();
		ExpressionFactory expFact = facesCtx.getApplication().getExpressionFactory();
		
		model = new DefaultMenuModel();
		Submenu sub = new Submenu();
		sub.setLabel("Hãng Sản Xuất");
		
		MenuItem itemall = new MenuItem();
		itemall.setTarget(tieude);
		itemall.setUpdate(":form:listsanpham");
		itemall.setValue("Tất Cả Sản Phẩm");
		itemall.setAjax(true);
		itemall.setActionExpression(expFact.createMethodExpression(elCtx, "#{beanBanHang.hang(\"tatca\")}", String.class, new Class[0]));
		sub.getChildren().add(itemall);
		List<NhaSanXuat> lnsx = DAO.laytatcanhasanxuat();
		for (NhaSanXuat nhaSanXuat : lnsx) {
			MenuItem item = new MenuItem();
			String hang = nhaSanXuat.getTennhasanxuat();
			
			item.setValue(hang);
			
			item.setUpdate(":form:listsanpham");
			item.setAjax(true);
			item.setActionExpression(expFact.createMethodExpression(elCtx, "#{beanBanHang.hang(\""+hang+"\")}", String.class, new Class[0]));
			sub.getChildren().add(item);
	        }
		model.addSubmenu(sub);
		
		
	}
	
	public void hang(String tenhang){
		if(tenhang.equals("tatca")){
			tieude = "Tất Cả Sản Phẩm";
			dssp = DAO.laytatcasanpham();
			return;
		}
		tieude = tenhang;
		dssp = DAO.timkiemtheohang(tenhang);
	}
	
	public void boSanPham(DragDropEvent ddEvent) {  
        selectedSanPham = ((SanPham) ddEvent.getData());
                     
        if(giohang.contains(selectedSanPham)){
        	for (int i = 0; i < giohang.size(); i++) {
				if(giohang.get(i).equals(selectedSanPham)){
					giohang.get(i).setSoLuong(giohang.get(i).getSoLuong()+1);
					tongsotien = tongsotien+(selectedSanPham.getGia().intValue()*(selectedSanPham.getSoLuong()-1));
					return;
				}
			}
        }
        else
        {
        	selectedSanPham.setSoLuong(1);
        	tongsotien = tongsotien+(selectedSanPham.getGia().intValue());
        }
        
        giohang.add(selectedSanPham);
        mapSua.put(selectedSanPham, false);
        
    }
	
	public int thanhtien(SanPham sp){
		int thanhtien = sp.getGia().intValue() *sp.getSoLuong();
		return thanhtien;
	}
	
	public long getTongsotien() {
		return tongsotien;
	}
	
	public String suasanpham(){
		tongsotien = 0;
		for (int i = 0; i < giohang.size(); i++) {
				tongsotien = tongsotien+(giohang.get(i).getGia().intValue()*(giohang.get(i).getSoLuong()));
		}
		mapSua.put(selectedSua, true);
		return null;
	}
	public String luusanpham(){
		if(selectedSua.getSoLuong() == 0){
			giohang.remove(selectedSua);
			mapSua.remove(selectedSua);
			tongsotien = 0;
			for (int i = 0; i < giohang.size(); i++) {
					tongsotien = tongsotien+(giohang.get(i).getGia().intValue()*(giohang.get(i).getSoLuong()));
			}
			return "muahang";
		}
		mapSua.put(selectedSua, false);
		return null;
	}
	public boolean sanphamsua(SanPham sp){
		return mapSua.get(sp);
	}
	
	public String xoasanpham(){
		for (int i = 0; i < giohang.size(); i++) {
			if(giohang.get(i).equals(selectedSua)){
				giohang.remove(i);
				break;
			}
		}
		tongsotien = 0;
		for (int i = 0; i < giohang.size(); i++) {
				tongsotien = tongsotien+(giohang.get(i).getGia().intValue()*(giohang.get(i).getSoLuong()));
		}
		if(giohang.isEmpty()){
			return "muahang";
		}
		return null;
	}
	public String lammoi(){
		tongsotien = 0;
		giohang.clear();
		mapSua.clear();
		return null;
	}
	
	public String giohang(){
		dataModel = new SanPhamDataModel(giohang);
		return "giohang";
	}
	public String luuhoadon(){
		DAO.luunguoidung(nguoidung);
		HoaDon hd = new HoaDon();
		hd.setNguoidung(nguoidung);
		Set<SanPham> ssp = new HashSet<SanPham>(giohang);
		hd.setDssp(ssp);
		hd.setTongtien(tongsotien);
		DAO.luuhoadon(hd);
		giohang.clear();
		mapSua.clear();
		dataModel = new SanPhamDataModel();
		tongsotien = 0;
		nguoidung = new NguoiDung();
		return "camon";
	}
	public String quaylai(){
		if(giohang.isEmpty()){
			return "muahang";
		}
		else{
			return "giohang";
		}
		
	}
}
